package com.ruoyi.web.controller.threeViolation;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Random;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.utils.pengjie.SnowflakeIdUtils;
import com.ruoyi.threeViolation.vo.ViolationPenalizeVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.threeViolation.domain.ViolationPenalize;
import com.ruoyi.threeViolation.service.IViolationPenalizeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 三违处罚Controller
 *
 * @author lth
 * @date 2023-12-18
 */
@RestController
@RequestMapping("/threeViolation/penalize")
public class ViolationPenalizeController extends BaseController
{
    @Autowired
    private IViolationPenalizeService violationPenalizeService;

    /**
     * 查询三违处罚列表
     */
    @PreAuthorize("@ss.hasPermi('threeViolation:penalize:list')")
    @GetMapping("/list")
    public TableDataInfo list(ViolationPenalize violationPenalize)
    {
        startPage();
        List<ViolationPenalize> list = violationPenalizeService.selectViolationPenalizeList(violationPenalize);
        return getDataTable(list);
    }

    /**
     * 导出三违处罚列表
     */
    @PreAuthorize("@ss.hasPermi('threeViolation:penalize:export')")
    @Log(title = "三违处罚", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ViolationPenalize violationPenalize)
    {
        List<ViolationPenalize> list = violationPenalizeService.selectViolationPenalizeList(violationPenalize);
        ExcelUtil<ViolationPenalize> util = new ExcelUtil<ViolationPenalize>(ViolationPenalize.class);
        util.exportExcel(response, list, "三违处罚数据");
    }

    /**
     * 获取三违处罚详细信息
     */
    @PreAuthorize("@ss.hasPermi('threeViolation:penalize:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(violationPenalizeService.selectViolationPenalizeById(id));
    }

    /**
     * 新增三违处罚
     */
    @PreAuthorize("@ss.hasPermi('threeViolation:penalize:add')")
    @Log(title = "三违处罚", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ViolationPenalize violationPenalize)
    {
        long id = new SnowflakeIdUtils(5, 8).nextId();
        violationPenalize.setId(String.valueOf(id));
        violationPenalize.setPenaltyBlacklist("HMD");
        String r = generateRandomString(3);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String currentDate = LocalDate.now().format(formatter);
        String code = "SW" + currentDate + r;
        violationPenalize.setVolationCode(code);
        return toAjax(violationPenalizeService.insertViolationPenalize(violationPenalize));
    }

    private static String generateRandomString(int length) {
        String chars = "0123456789abcdefghijklmnopqrstuvwxyz";
        StringBuilder sb = new StringBuilder(length);
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            char c = chars.charAt(random.nextInt(chars.length()));
            sb.append(c);
        }
        return sb.toString();
    }


    /**
     * 修改三违处罚
     */
    @PreAuthorize("@ss.hasPermi('threeViolation:penalize:edit')")
    @Log(title = "三违处罚", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ViolationPenalize violationPenalize)
    {
        return toAjax(violationPenalizeService.updateViolationPenalize(violationPenalize));
    }

    /**
     * 删除三违处罚
     */
    @PreAuthorize("@ss.hasPermi('threeViolation:penalize:remove')")
    @Log(title = "三违处罚", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(violationPenalizeService.deleteViolationPenalizeByIds(ids));
    }


    /**
     * 三违处罚列表
     */
    @PreAuthorize("@ss.hasPermi('threeViolation:penalize:list')")
    @GetMapping("/penalizeList")
    public TableDataInfo penalizeList(ViolationPenalizeVo vo)
    {
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        List<ViolationPenalize> list = violationPenalizeService.penalizeList(vo);
        return getDataTable(list);
    }

    /**
     * 三违处罚黑名单列表
     */
    @PreAuthorize("@ss.hasPermi('threeViolation:penalize:list')")
    @GetMapping("/blackList")
    public TableDataInfo blackList(ViolationPenalizeVo vo)
    {
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        List<ViolationPenalize> list = violationPenalizeService.blackList(vo);
        return getDataTable(list);
    }




}
